package com.xxhui.custombardemo.View;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xxhui.custombardemo.Interface.CustomBarClickListener;
import com.xxhui.custombardemo.R;

/**
 * Created by Administrator on 2016/7/13.
 */
public class CustomBar extends RelativeLayout {

    // 包含topbar上的元素：左按钮、右按钮、标题
    private Button left_but;
    private Button right_but;
    private TextView title_tv;

    //只是方便使用而已，实际项目配置并没有这个id的
    public static final int TIELTE_ID = 0x7f110041;
    public static final int LEFT_BUT_ID = 0x7f110042;
    public static final int RIGHT_BUT_ID = 0x7f110043;

    // 布局属性，用来控制组件元素在ViewGroup中的位置
    private LayoutParams title_params;
    private LayoutParams left_params;
    private LayoutParams right_params;

    // 标题的属性值，即我们在atts.xml文件中定义的属性
    private String title_res;
    private int title_color;
    private float title_textSize;

    // 左按钮的属性值，即我们在atts.xml文件中定义的属性
    private String left_text;
    private int left_color;
    private Drawable left_background;

    // 右按钮的属性值，即我们在atts.xml文件中定义的属性
    private String right_text;
    private int right_color;
    private Drawable right_background;

    // 设置传入的接口对象
    private CustomBarClickListener clickListener;

    public CustomBar(Context context) {
        super(context);
    }

    public CustomBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initRes(context, attrs);
        createView(context);
        left_but.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (clickListener != null) {
                    clickListener.leftClick();
                }
            }
        });
        right_but.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (clickListener != null) {
                    clickListener.rightClick();
                }
            }
        });
    }

    private void initRes(Context context, AttributeSet attrs) {
        // 设置topbar的背景
        setBackgroundColor(0xFFF59563);
        //通过这个方法，将attrs.xml自定义的declare-styleable
        //的所有属性的值存储到TypeArray中
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomBar);

        // 从TypedArray中取出对应的值来为要设置的属性赋值
        title_res = array.getString(R.styleable.CustomBar_title);
        title_textSize = array.getDimension(R.styleable.CustomBar_title_textSize, 10);
        title_color = array.getColor(R.styleable.CustomBar_title_textColor, 0);

        left_text = array.getString(R.styleable.CustomBar_left_text);
        left_color = array.getColor(R.styleable.CustomBar_left_textColor, 10);
        left_background = array.getDrawable(R.styleable.CustomBar_left_background);

        right_text = array.getString(R.styleable.CustomBar_right_text);
        right_color = array.getColor(R.styleable.CustomBar_right_textColor, 10);
        right_background = array.getDrawable(R.styleable.CustomBar_right_background);
        array.recycle();
    }

    /**
     * 动态生成布局
     *
     * @param context
     */
    private void createView(Context context) {
        //实例化控件
        title_tv = new TextView(context);
        left_but = new Button(context);
        right_but = new Button(context);
        //为标题TextView添加属性
        title_tv.setText(title_res);
        title_tv.setTextColor(title_color);
        title_tv.setTextSize(title_textSize);
        title_tv.setGravity(Gravity.CENTER);
        //为左Button添加属性
        left_but.setText(left_text);
        left_but.setTextColor(left_color);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            left_but.setBackground(left_background);
        }
        //为右Button添加属性
        right_but.setText(right_text);
        right_but.setTextColor(right_color);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            right_but.setBackground(right_background);
        }
        //创建布局属性，并将控件，按照布局属性，添加到父容器
        title_params = new LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        title_params.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
        addView(title_tv, title_params);
        //同上
        left_params = new LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        left_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
        addView(left_but, left_params);
        //同上
        right_params = new LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        right_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
        addView(right_but, right_params);
    }

    public void setButtonVisible(int id, boolean flag) {
        if (flag) {
            switch (id) {
                case LEFT_BUT_ID:
                    left_but.setVisibility(View.VISIBLE);
                    break;
                case RIGHT_BUT_ID:
                    right_but.setVisibility(View.VISIBLE);
                    break;
                default:
                    break;
            }
        } else {
            switch (id) {
                case LEFT_BUT_ID:
                    left_but.setVisibility(View.GONE);
                    break;
                case RIGHT_BUT_ID:
                    right_but.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        }
    }

    public void setClickListener(CustomBarClickListener clickListener) {
        this.clickListener = clickListener;
    }
}
